<template>
  <!-- 修改资料 -->
  <div>
    <!-- 内容主题区域 -->
    <el-form
      label-width="75px"
      :model="form"
      label-position="right"
      ref="formRef"
      :rules="formRules"
    >
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="编号" prop="number">
            <el-input
              style="width: 70%"
              v-model="form.number"
              placeholder="请输入模板标识"
            ></el-input
            >&nbsp;
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="模板名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入模板名称"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="功能模块" prop="metaModuleId">
            <ts-select
              v-model="form.metaModuleId"
              fileName="MetaModuleReport"
              :template="tableProps"
              @updateRow="metaModuleChange"
            ></ts-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="上传附件" prop="file">
            <tsUpload
              type="file"
              :image-url="imageUrl"
              @updateFile="updateFile"
              @removeFile="removeFile"
            >
            </tsUpload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="状态" prop="status">
            <el-switch
              v-model="form.status"
              active-value="normal"
              inactive-value="disable"
            ></el-switch>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="备注" prop="remark">
            <el-input
              style="width: 100%"
              v-model="form.remark"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <sc-title title="表字段列表"></sc-title>
      <el-scrollbar height="400px">
        <sc-form-table
          v-model="dataList"
          :addTemplate="addTemplate"
          edit
          @delete="rowDelete"
          placeholder="暂无数据"
        >
          <template #tableContent>
            <el-table-column prop="number" label="表名">
              <template #default="scope">
                <span v-if="scope.row.operationType == ''">{{
                  scope.row.number
                }}</span>
                <el-input
                  v-else
                  v-model="scope.row.number"
                  placeholder="请输入数据集标识"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="表描述">
              <template #default="scope">
                <span v-if="scope.row.operationType == ''">{{
                  scope.row.name
                }}</span>
                <el-input
                  v-else
                  v-model="scope.row.name"
                  placeholder="请输入名称"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="关联类型">
              <template #default="scope">
                <span v-if="scope.row.operationType == ''">{{
                  scope.row.relationTypeName
                }}</span>
                <el-select
                  v-else
                  v-model="scope.row.relationType"
                  clearable
                  placeholder="请选择："
                >
                  <el-option
                    v-for="item in configData.relationTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="记录相同的字段">
              <template #default="scope">
                <span v-if="scope.row.operationType == ''">{{
                  scope.row.uniqueFields
                }}</span>
                <el-select
                  v-else
                  v-model="scope.row.uniqueFieldsArray"
                  clearable
                  placeholder="请选择："
                  collapse-tags
                  max-collapse-tags="2"
                >
                  <el-option
                    v-for="item in scope.row.fieldList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column prop="refMainTableName" label="关联主表名">
              <template #default="scope">
                <span v-if="scope.row.operationType == ''">{{
                  scope.row.refMainTableName
                }}</span>
                <el-input
                  v-else
                  v-model="scope.row.refMainTableName"
                  placeholder="关联主表名"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="refMainTableField" label="关联字段">
              <template #default="scope">
                <span v-if="scope.row.operationType == ''">{{
                  scope.row.refMainTableField
                }}</span>
                <el-input
                  v-else
                  v-model="scope.row.refMainTableField"
                  placeholder="关联字段"
                ></el-input>
              </template>
            </el-table-column>
          </template>
        </sc-form-table>
      </el-scrollbar>
    </el-form>
    <div class="button-position">
      <el-button @click="updateVisible">取 消</el-button>
      <el-button type="primary" @click="updatePage(formRef)">确 定</el-button>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { ElNotification } from "element-plus";
import { Check, Close } from "@element-plus/icons-vue";
import Api from "@/api/model/system";
import tsSelect from "@/components/scSelect/index.vue";
import tsUpload from "@/components/tsUpload/file.vue";
const emit = defineEmits(["updatetable"]);
// 添加用户数据的对象
const form = ref({});
const configData = ref({});
const addTemplate = ref({});
const dataList = ref([]);
const deleteList = ref([]);
const formRef = ref();
const tableProps = {
  value: "id",
  label: "name",
};
const imageUrl = reactive({
  name: "",
  url: "",
});
const reportFileNameList = ref([]);
const props = defineProps({
  paramsData: {
    type: Object,
    defalut: {},
  },
});
onMounted(() => {
  getInit();
});

const getInit = async () => {
  let formRes = await Api.excel.configDetail.post(props.paramsData);
  if (formRes.code == 200) {
    imageUrl.url = formRes.data.fileFullPath;
    imageUrl.name = formRes.data.fileName;
    form.value = formRes.data;
    configData.value = formRes.configData;
    addTemplate.value = formRes.dtl;
    dataList.value = formRes.dataList;
  }
};
const rowDelete = (val) => {
  deleteList.value.push(val);
};

const restAddFormData = () => {
  formRef.value.resetFields();
};

const updateVisible = (val) => {
  emit("updatetable");
};
const updateFile = (val) => {
  form.value.file = val;
};
const removeFile = () => {
  form.value.file = undefined;
};
const metaModuleChange = async (val) => {
  let res = await Api.excel.searchConfigFiles.post({ id: val.id });
  if (res.code == 200) {
    reportFileNameList.value = res.dataList;
  }
};
// 上传
const updatePage = async (formRef) => {
  await formRef.validate((valid, fields) => {
    if (valid) {
      updatePageData();
    }
  });
};

const updatePageData = async () => {
  let params = {
    bean: form.value,
    dataList: dataList.value.concat(deleteList.value).filter((item, index) => {
      return delete item.fieldList;
    }),
  };
  let res = await Api.excel.configUpdate.post(params);
  if (res.code == 200) {
    restAddFormData();
    ElNotification.success({
      title: "",
      message: "添加成功！",
      duration: 2000,
    });
    emit("updatetable", true);
  }
};
</script>

<style lang="less" scoped></style>
